package top.zywork.service.impl;

import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zywork.common.BeanUtils;
import top.zywork.dao.ActivityDAO;
import top.zywork.dao.ActivityRanksDAO;
import top.zywork.dao.RanksUserDAO;
import top.zywork.dos.ActivityDO;
import top.zywork.dto.ActivityDTO;
import top.zywork.dto.ActivityRanksDTO;
import top.zywork.dto.RanksUserDTO;
import top.zywork.service.AbstractBaseService;
import top.zywork.service.ActivityService;
import top.zywork.vo.ActivityVO;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * ActivityServiceImpl服务接口实现类<br/>
 *
 * 创建于2020-05-13<br/>
 *
 * @author http://zywork.top 危锦辉
 * @version 1.0
 */
@Service(value = "activityService")
public class ActivityServiceImpl extends AbstractBaseService implements ActivityService {

    private ActivityDAO activityDAO;

    private ActivityRanksDAO activityRanksDAO;

    private RanksUserDAO ranksUserDAO;

    @Autowired
    public void setActivityDAO(ActivityDAO activityDAO) {
        super.setBaseDAO(activityDAO);
        this.activityDAO = activityDAO;
    }

    @Autowired
    public void setActivityRanksDAO(ActivityRanksDAO activityRanksDAO) {
        this.activityRanksDAO = activityRanksDAO;
    }

    @Autowired
    public void setRanksUserDAO(RanksUserDAO ranksUserDAO) {
        this.ranksUserDAO = ranksUserDAO;
    }

    @PostConstruct
    public void init() {
        super.init(ActivityDO.class, ActivityDTO.class);
    }

    @Transactional
    @Override
    public void saveData(ActivityDTO activityDTO) {
        String[] postionArr = new String[]{"上","中","野","下","辅"};
        activityDAO.save(activityDTO);
        Long activityId = activityDTO.getId();
        String type = "蓝方";
        ActivityRanksDTO activityRanksDTO = ActivityRanksDTO.builder().activityId(activityId).name(activityDTO.getBlueRanksName()).type(type).build();
        activityRanksDAO.save(activityRanksDTO);
        Long activityRankId = activityRanksDTO.getId();
        List<RanksUserDTO> ranksUserDTOList = new ArrayList<>();
        for (String postion : postionArr) {
            RanksUserDTO ranksUserDTO = RanksUserDTO.builder().activityRanksId(activityRankId).position(postion).build();
            ranksUserDTOList.add(ranksUserDTO);
        }
        String type1 = "红方";
        ActivityRanksDTO activityRanksDTO1 = ActivityRanksDTO.builder().activityId(activityId).name(activityDTO.getRedRanksName()).type(type1).build();
        activityRanksDAO.save(activityRanksDTO1);
        Long activityRankId1 = activityRanksDTO1.getId();
        for (String postion : postionArr) {
            RanksUserDTO ranksUserDTO = RanksUserDTO.builder().activityRanksId(activityRankId1).position(postion).build();
            ranksUserDTOList.add(ranksUserDTO);
        }
        ranksUserDAO.saveBatch(BeanUtils.copyListObj(ranksUserDTOList, RanksUserDTO.class));
    }

    @Override
    public List<ActivityVO> getAllActivity() {
        List<Object> objectList = activityDAO.getAllActivity();
        if (objectList.isEmpty()) {
            return null;
        }
        List<ActivityVO> activityVOList = BeanUtils.copy(objectList, ActivityVO.class);
        return activityVOList;
    }

    @Override
    public List<ActivityVO> getAllUserActivity(Long userId) {
        List<Object> objectList = activityDAO.getAllUserActivity(userId);
        if (objectList.isEmpty()) {
            return null;
        }
        List<ActivityVO> activityVOList = BeanUtils.copy(objectList, ActivityVO.class);
        return activityVOList;
    }
}